package org.seven.jrdp.web.t;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.seven.jrdp.biz.t.model.TCkd;
import org.seven.jrdp.biz.t.service.TCkdService;
import org.seven.jrdp.commons.model.Result;
import org.seven.jrdp.commons.mybatis.model.PageRequest;
import org.seven.jrdp.commons.util.Excel2007Utils;
import org.seven.jrdp.commons.util.ServletUtils;
import org.seven.jrdp.web.t.plugins.ExcelPlugin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/mgt/t/fhd")
public class TFhdController {
	private static final String EXCEL_FHD = "fhd.xlsx";
	@Autowired
	private TCkdService tCkdService;

	@GetMapping("/{view}")
	public ModelAndView view(@PathVariable String view) {
		return new ModelAndView("mgt/t/fhd/fhd_" + view);
	}

	@PostMapping("/find")
	public PageInfo<Map<String, Object>> find(PageRequest pageRequest, HttpServletRequest request) {
		Map<String, Object> map = ServletUtils.getSearchParams(request);
		PageInfo<Map<String, Object>> page = tCkdService.findFhd(map, pageRequest);
		return page;
	}

	@PostMapping("/update")
	public Result update(TCkd tCkd) {
		tCkdService.update(tCkd);
		return Result.SUCCESS;
	}

	@RequestMapping("/dc")
	public void dc(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = ServletUtils.getSearchParams(request);
		List<Map<String, Object>> list = tCkdService.findDc(map);
		Workbook workbook = Excel2007Utils.getWorkbook(ServletUtils.getExcelPath(EXCEL_FHD));
		dc(workbook, list);
		String fileName = "发货单" + Excel2007Utils.SUFFIX;
		ServletUtils.download(workbook, fileName, request, response);
	}

	@SuppressWarnings("unchecked")
	private void dc(Workbook workbook, List<Map<String, Object>> list) {
		ExcelPlugin plugin = new ExcelPlugin(workbook);
		Sheet sheet = workbook.getSheetAt(0);
		int index = 4;
		for (int i = 0; i < list.size(); i++) {
			Map<String, Object> fhd = list.get(i);
			Row row = sheet.createRow(index + i);
			row.setHeightInPoints(20f);
			row.createCell(0).setCellStyle(plugin.FONT10);
			row.createCell(1).setCellStyle(plugin.FONT10);
			row.createCell(2).setCellStyle(plugin.FONT10);
			row.createCell(3).setCellStyle(plugin.FONT10);
			row.createCell(4).setCellStyle(plugin.FONT10);
			row.createCell(5).setCellStyle(plugin.FONT10);
			row.createCell(6).setCellStyle(plugin.FONT10);
			row.createCell(7).setCellStyle(plugin.FONT10);
			row.createCell(8).setCellStyle(plugin.FONT10);
			row.getCell(0).setCellValue(i + 1);
			row.getCell(1).setCellValue(DateFormatUtils.format((Timestamp) fhd.get("ckrq"), "M月d日"));
			row.getCell(2).setCellValue(DateFormatUtils.format((Timestamp) fhd.get("ddrq"), "M月d日"));
			if (fhd.get("nrrq") != null) {
				row.getCell(3).setCellValue(DateFormatUtils.format((Timestamp) fhd.get("nrrq"), "M月d日"));
			}
			if (fhd.get("cc") != null) {
				row.getCell(4).setCellValue((Integer) fhd.get("cc"));
			}
			row.getCell(5).setCellValue((Integer) fhd.get("zl"));
			row.getCell(6).setCellValue((String) fhd.get("mx"));
			if (fhd.get("xh") != null) {
				row.getCell(7).setCellValue((String) fhd.get("xh"));
			}
			if (fhd.get("ch") != null) {
				row.getCell(8).setCellValue((String) fhd.get("ch"));
			}
			List<String> lhs = (List<String>) fhd.get("lh");
			for (int j = 0; j < lhs.size(); j++) {
				String lh = lhs.get(j);
				row.createCell(9 + j).setCellStyle(plugin.FONT10);
				row.getCell(9 + j).setCellValue(lh);
			}
		}
	}
}